from django.apps import AppConfig
import os
import sys
import threading

class LinkConfig(AppConfig):
    name = 'link'
    _initialized = False
    _lock = threading.Lock()

    def ready(self):
        is_django_server = any('runserver' in arg for arg in sys.argv)
        print("+++++++++LinkConfig+++++++++ready++++++++++++++")
        # 留空，不在这里初始化组件
        # Django会在需要时自动调用project中的懒加载函数
        with self._lock:
            if not self._initialized and is_django_server:
                print("+++++++++LinkConfig+++++++++hourly_task++++++++++++++")
                from link.api import hourly_task
                hourly_task.delay()  # 启动时触发一次
                self._initialized = True

print(f"+++++++++++++{LinkConfig._initialized}+++++++++++++++++")
